package com.note.simpleadmin.module.system.controller;

import com.note.simpleadmin.constant.ErrorCode;
import com.note.simpleadmin.entity.TbEnvironment;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.exception.DataRepeatException;
import com.note.simpleadmin.module.system.service.EnvironmentService;
import com.note.simpleadmin.vo.Msg;
import com.note.simpleadmin.vo.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/***
 *@ClaseName:IndexController
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/27 0027 17:57
 */
@Controller
@RequestMapping(value = "system/dict")
public class DictController {
    private final  Logger logger = LoggerFactory.getLogger(DictController.class);
    @Autowired
    private EnvironmentService environmentService;
    @RequestMapping(value = "index")
    public String index(HttpServletRequest request)
    {
        return "system/setting/dict";
    }
    @RequestMapping(value = "ajax_list")
    @ResponseBody
    public Msg ajax_list(String type,String key,Integer page)
    {

        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            PageBean<TbEnvironment> pageBean=environmentService.findByPage(type,key,page);
            msg.setData(pageBean);
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "addDictUI")
    public String addDictUI(HttpServletRequest request)
    {
        return "system/setting/addDict";
    }
    @RequestMapping(value = "ajax_add")
    @ResponseBody
    public Msg ajax_add(TbEnvironment environment) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            environmentService.add(environment);
            msg.setMsg("添加成功");
        } catch(DataRepeatException e) {
            msg.setMsg(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "ajax_del")
    @ResponseBody
    public Msg ajax_del(String id) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            environmentService.delete(id);
            msg.setMsg("删除成功");
        } catch(DataNotExistException e) {
            msg.setMsg(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "updateDictUI")
    public String updateDictUI(String id,HttpServletRequest request)
    {
        TbEnvironment environment=environmentService.getOne(id);
        request.setAttribute("environment",environment);
        return "system/setting/editDict";
    }
    @RequestMapping(value = "ajax_update")
    @ResponseBody
    public Msg ajax_update(TbEnvironment environment) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            environmentService.update(environment);
            msg.setMsg("修改成功");
        } catch(DataNotExistException e) {
            msg.setMsg(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
}
